package com.baosight.risk.service.riaa.impl;

import com.baosight.risk.api.riaa.RiaaEvaluateService;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.riaa.RiaaEvaluateMapper;
import com.baosight.risk.service.riaa.entity.RiaaEvaluate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RiaaEvaluateServiceImpl extends BaseServiceImpl<RiaaEvaluate> implements RiaaEvaluateService {

    @Autowired
    private RiaaEvaluateMapper riaaEvaluateMapper;


    @Override
    public List<RiaaEvaluate> selectFieldList(RiaaEvaluate riaaEvaluate) {
        return riaaEvaluateMapper.selectFieldList(riaaEvaluate);
    }


    @Override
    public List<RiaaEvaluate> selectDetailList(String parentId) {
        return riaaEvaluateMapper.selectDetailList(parentId);
    }

    @Override
    public Integer saveEvaluate(List<RiaaEvaluate> riaaEvaluateList) {
        for (RiaaEvaluate riaaEvaluate : riaaEvaluateList) {
            if (StringUtils.isEmpty(riaaEvaluate.getId())) {
                riaaEvaluate = (RiaaEvaluate) BeanUtils.initBean(riaaEvaluate);
                riaaEvaluateMapper.insert(riaaEvaluate);
            } else {
                riaaEvaluate = (RiaaEvaluate) BeanUtils.updateBean(riaaEvaluate);
                riaaEvaluateMapper.updateByPrimaryKey(riaaEvaluate);
            }
        }
        return 1;
    }

    @Override
    public Integer deleteByIds(MultipleUpdateStatus updateStatus) {
        for (String id : updateStatus.getIds()) {
            RiaaEvaluate riaaEvaluate = riaaEvaluateMapper.selectByPrimaryKey(id);
            if ("0".equals(riaaEvaluate.getParentId())) {
                riaaEvaluateMapper.deleteByParentId(riaaEvaluate.getId());
            }
            riaaEvaluateMapper.deleteByPrimaryKey(riaaEvaluate.getId());
        }
        return 1;
    }

}
